package com.android.examplelist;

import net.simonvt.messagebar.MessageBar;
import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

public class MessageBarActivity extends Activity implements MessageBar.OnMessageClickListener {

    private static final String STATE_MESSAGEBAR = "net.simonvt.messagebar.samples.SampleActivity.messageBar";
    private static final String STATE_COUNT = "net.simonvt.messagebar.samples.SampleActivity.count";

    private MessageBar mMessageBar;

    private TextView mTextView;

    private int mCount;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.message_bar);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		
		
        SpinnerAdapter adapter = ArrayAdapter.createFromResource(this, 
                R.array.city, android.R.layout.simple_spinner_dropdown_item);
         
        ActionBar actionBar = getActionBar();
        
        actionBar.setDisplayShowTitleEnabled(false); 
        
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        actionBar.setListNavigationCallbacks(adapter, new OnNavigationListener() {
 
            @Override
            public boolean onNavigationItemSelected(int itemPosition, long itemId) {
                // TODO Auto-generated method stub
                return false;
            }
             
        });
		
		
		
        mTextView = (TextView) findViewById(R.id.messageClickedTextView);
        
       
        mMessageBar = new MessageBar(this);
       mMessageBar.setOnClickListener(this);
        
        findViewById(R.id.withText).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMessageBar.show("Message #" + mCount);
                mCount++;
            }
        });

        findViewById(R.id.withTextAndButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle b = new Bundle();
                b.putInt("count", mCount);
                mMessageBar.show("Message #" + mCount, "Button!", R.drawable.ic_messagebar_undo, b);
                mCount++;
            }
        });
		
		
	}
	
	
    @Override
    protected void onRestoreInstanceState(Bundle inState) {
        super.onRestoreInstanceState(inState);
        mMessageBar.onRestoreInstanceState(inState.getBundle(STATE_MESSAGEBAR));
        mCount = inState.getInt(STATE_COUNT);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBundle(STATE_MESSAGEBAR, mMessageBar.onSaveInstanceState());
        outState.putInt(STATE_COUNT, mCount);
    }

    @Override
    public void onMessageClick(Parcelable token) {
        Bundle b = (Bundle) token;
        final int count = b.getInt("count");
        mTextView.setText("You clicked message #" + count);
    }
    

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case android.R.id.home:
				finish();
				return true;
			case R.id.action_settings:
				return true;
		}
		
		return super.onOptionsItemSelected(item);
	 }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
